home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
Libris Britannia 4
/
science library(b).zip
/
science library(b)
/
UTILITIE
/
CONVERSI
/
1396.ZIP
/
DATETRAN.C
< prev
next >
Wrap
C/C++ Source or Header
|
1988-03-20
|
12KB
|
437 lines
/*
* datetran.c
*
* Translate various forms of dates into other forms
*
* Usage is :
* datetran in_file out_file in_format out_format
*
* 1) in_file is the file to be translated
* 2) out_file is the file to be created
* 3) in_format is the way the dates look in 'in_file' that you want changed
* 4) out_format is the way you want the dates to look in 'out_file'
*
* in_format and out_format are formed as follows :
* 1) the format must be exactly 8 characters long
* 2) the letters DD or dd must appear in the format
* 3) the letters MM or mm must appear in the format
* 4) the letters YY or yy must appear in the format
* 5) some form of punctuation must appear in the 3rd & 6th
* positions of the format
* examples :
* mm/dd/yy mm.dd.yy yy*dd*mm MMxDDxYY
* note that you are restricted ONLY in that you may note use
* the letters 'm', 'd', 'y', (or their upper case versions) as
* punctuation, and that both punctuations must be the same
*
* Recommended compilation procedure using Manx Aztec C86-d :
* cc -E1000 -Z3000 datetran.c
* ln datetran.o -lc
*
* Released to the public domain March 20, 1988
* Bill Leary
* Co-Sysop, Cul-de-Sac TBBS, Holliston, Mass.
* (617)429-1784 300/1200/2400 baud, 8 bits, 1 stop bit, no parity
*/
#include <stdio.h>
#include <ctype.h>
extern long ftell();
void usage( s )
char *s;
{
fprintf( stderr, "\nERROR : %s\n", s );
fprintf( stderr, "\n Try datetran help\n\007" );
exit( -1 );
}
void help()
{
printf( "\n\n\n\n\n\n" );
printf( "DATETRAN :\n" );
putchar( '\n' );
printf( " This program will read a text file line by line and change\n" );
printf( "all occurances of dates in one user specified form into\n" );
printf( "dates of another user specified form.\n" );
putchar( '\n' );
printf( " The program expects to be given four parameters :\n" );
printf( " 1) The name of the file to be converted.\n" );
printf( " 2) The name of a file the program will create\n" );
printf( " which will become a copy of the original file\n" );
printf( " except for the changed formats of the dates.\n" );
printf( " 3) A format (or 'picture') of how the dates in\n" );
printf( " the original file are now formed.\n" );
printf( " 4) A format of how you want the dates in\n" );
printf( " the new file to be formed.\n" );
putchar( '\n' );
printf( " The correct way to start this program is :\n" );
putchar( '\n' );
printf( " datetran original_file new_file original_format new_format\n" );
printf( "\n-- press Enter to continue --" );
fflush( stdout );
while ( getchar() != '\n' )
;
printf( "\n\n\n\n\n\n" );
printf( " The file names are the usual forms of file names, including\n" );
printf( "full support for paths.\n" );
putchar( '\n' );
printf( " The rules for files are :\n" );
printf( " 1) The original_file must already exist.\n" );
printf( " 2) The new_file must not exist.\n" );
printf( " 3) The original and new files must not be the same.\n" );
putchar( '\n' );
printf( " The rules for date formats (or 'pictures') are :\n" );
printf( " 1) The characters 'DD' or 'dd' must appear in the format.\n" );
printf( " 2) The characters 'MM' or 'mm' must appear in the format.\n" );
printf( " 3) The characters 'YY' or 'yy' must appear in the format.\n" );
printf( " 4) Each of the above character pairs must only be used\n" );
printf( " once in the format.\n" );
printf( " 5) Some form of punctuation must appear between each of\n" );
printf( " the character pairs.\n" );
printf( " 6) The characters 'M', 'm', 'D', 'd', 'Y' and 'y' are not\n" );
printf( " permitted as punctuation in the format.\n" );
printf( " 7) The two punctuation characters must be the same.\n" );
putchar( '\n' );
printf( " good examples : MM/DD/YY yy-dd-mm DDxMMxYY MM*DD*YY\n" );
printf( " bad examples : M/DD/YYY MMmDDmYY mm-dd/yy DD/DD/YY\n" );
printf( "\n-- press Enter to continue --" );
fflush( stdout );
while ( getchar() != '\n' )
;
printf( "\n\n\n\n\n\n\n" );
printf( "Example of use :\n" );
putchar( '\n' );
printf( " datetran alpha.a beta.b mm/dd/yy yy-mm-dd\n" );
putchar( '\n' );
printf( " Would read every line in 'alpha.a' and copy it to 'beta.b'.\n" );
printf( " In the process any dates like 02/23/88 or 09/02/55 would\n" );
printf( " translated into 88-02-23 and 55-09-02.\n" );
putchar( '\n' );
printf( "CAUTION : This program doesn't actually check to see that\n" );
printf( " the parts of the date are legal. That is, it believes that\n" );
printf( " any sequence of six digits, broken up by the punctuation\n" );
printf( " you gave in 'original_format' is a date. It then uses the\n" );
printf( " MM, DD and YY to decide how you want the digits swapped\n" );
printf( " around. In the above example the sequence 88/99/11 would\n" );
printf( " have been converted to 11-88-99.\n" );
putchar( '\n' );
printf( "end of help\n" );
exit( 0 );
}
/*
* issues an update to the operator telling him/her how we're doing
*/
void show_count( l, c )
int l, c;
{
printf( "\rlines = %d, corrections = %d", l, c );
fflush( stdout );
}
/*
* returns -1 if given strings are equal
* returns 0 otherwise
*
* Is case insensitive
*/
int strckeq( a, b )
char *a, *b;
{
while ( *a )
{
if ( toupper( *a++ ) != toupper( *b++ ) )
return 0;
}
if ( *a || *b )
return 0;
return -1;
}
void prep_date( date, MM_pos, DD_pos, YY_pos, P_char )
char *date;
int *MM_pos, *DD_pos, *YY_pos;
char *P_char;
{
int i;
char temp[80];
*MM_pos = 10;
*DD_pos = 10;
*YY_pos = 10;
*P_char = '\0';
for ( i = 0; i < 8; i++ )
{
if ( *(date+i) == 'M' )
{
if ( *(date+i+1) == 'M' )
{
if ( *MM_pos != 10 )
goto hopeless;
if ( ( i != 0 ) && ( i != 3 ) && ( i != 6 ) )
goto hopeless;
*MM_pos = i;
}
if ( *MM_pos == 10 )
goto hopeless;
continue;
}
if ( *(date+i) == 'D' )
{
if ( *(date+i+1) == 'D' )
{
if ( *DD_pos != 10 )
goto hopeless;
if ( ( i != 0 ) && ( i != 3 ) && ( i != 6 ) )
goto hopeless;
*DD_pos = i;
}
if ( *DD_pos == 10 )
goto hopeless;
continue;
}
if ( *(date+i) == 'Y' )
{
if ( *(date+i+1) == 'Y' )
{
if ( *YY_pos != 10 )
goto hopeless;
if ( ( i != 0 ) && ( i != 3 ) && ( i != 6 ) )
goto hopeless;
*YY_pos = i;
}
if ( *YY_pos == 10 )
goto hopeless;
continue;
}
if ( *P_char && ( *P_char != *(date+i) ) )
goto hopeless;
if ( ( i != 2 ) && ( i != 5 ) )
goto hopeless;
*P_char = *(date+i);
}
if ( ( *MM_pos == 10 ) || ( *DD_pos == 10 ) || ( *YY_pos == 10 ) )
goto hopeless;
if ( *P_char )
return;
hopeless:
sprintf( temp, "%s isn't a correct date format", date );
usage( temp );
}
main( argc, argv)
int argc;
char *argv[];
{
FILE *in_file, *out_file;
char line[1000];
char c;
char *cp;
int i, j;
int ic;
long position;
int exchange;
int lines = 0;
int corrections = 0;
char old[10];
char new[10];
int old_MM_pos;
int old_DD_pos;
int old_YY_pos;
char old_P_char;
int new_YY_pos;
int new_MM_pos;
int new_DD_pos;
char new_P_char;
/*
* check for attempt to get help first
*/
if ( ( argc == 2 ) && ( strlen( argv[1] ) == 4 ) )
{
for ( i = 0; i < strlen( argv[1] ); i++ )
if ( islower( argv[1][i] ) )
new[i] = toupper( argv[1][i] );
else
new[i] = argv[1][i];
new[i] = '\0';
if ( strcmp( new, "HELP" ) == 0 )
help();
}
/*
* make sure
* there are EXACTLY FOUR arguments
* the first and second are file names
* the two file names are NOT the same
* the first file DOES exist
* the second file DOESN'T exist
* the third and fourth are date formats
* the two date formats are NOT the same
*/
if ( argc < 5 )
usage( "not enough arguments" );
if ( argc > 5 )
usage( "too many arguments" );
if ( strckeq( argv[1], argv[2] ) )
usage( "source and destination file names are the same" );
if ( access( argv[1], 0 ) )
{
sprintf( line, "can't find %s", argv[1] );
usage( line );
}
if ( access( argv[1], 4 ) )
{
sprintf( line, "can't open %s for read", argv[1] );
usage( line );
}
if ( access( argv[2], 0 ) == 0 )
{
sprintf( line, "%s already exists", argv[2] );
usage( line );
}
if ( strlen( argv[3] ) != 8 )
{
sprintf( line, "old_format, %s,isn't 8 characters long", argv[3] );
usage( line );
}
if ( strlen( argv[4] ) != 8 )
{
sprintf( line, "new_format, %s,isn't 8 characters long", argv[4] );
usage( line );
}
/*
* convert formats to uppercase and store locally
*/
for ( i = 0; i < 8; i++ )
{
old[i] = *(argv[3]+i);
if ( islower( old[i] ) )
old[i] = toupper( old[i] );
new[i] = *(argv[4]+i);
if ( islower( new[i] ) )
new[i] = toupper( new[i] );
}
old[i] = '\0';
new[i] = '\0';
/*
* validate old and new date formats and set up for conversions
*/
prep_date( old, &old_MM_pos, &old_DD_pos, &old_YY_pos, &old_P_char );
prep_date( new, &new_MM_pos, &new_DD_pos, &new_YY_pos, &new_P_char );
/*
* make sure date formats aren't the same
*/
if ( strcmp( old, new ) == 0 )
usage( "both date formats are the same" );
/*
* open source file for read only
*/
if ( ( in_file = fopen( argv[1], "r" ) ) == (FILE *)0 )
{
sprintf( line, "can't open %s", argv[1] );
usage( line );
}
/*
* open destination file for write only
*/
if ( ( out_file = fopen( argv[2], "w" ) ) == (FILE *)0 )
{
sprintf( line, "can't create %s", argv[2] );
usage( line );
}
/*
* tell operator what we're doing
*/
printf( "\nInput file : %s\n", argv[1] );
printf( "Output file: %s\n", argv[2] );
printf( "Translating %s to %s\n", old, new );
show_count( lines, corrections );
/*
* read each line, replacing any old format dates with new
* format dates
*/
ic = 'x';
while ( 1 )
{
/*
* if we saw end of file last time around, we're done
*/
if ( ic == EOF )
break;
/*
* get ready to read line
*/
i = 0;
cp = (char *)0;
/*
* read line
*/
while ( ( ic = fgetc( in_file ) ) != EOF )
{
if ( ic == '\r' )
continue;
line[i++] = (char)ic;
if ( ic == '\n' )
break;
}
line[i] = '\0';
if ( ic != EOF )
lines++;
/*
* find date patterns in this line
*/
for ( i = 0; i < strlen( line ) - 8; i++ )
{
if ( isdigit( line[i] ) && isdigit( line[i+1] )
&& isdigit( line[i+3] ) && isdigit( line[i+4] )
&& isdigit( line[i+6] ) && isdigit( line[i+7] )
&& ( line[i+2] == old_P_char )
&& ( line[i+5] == old_P_char ) )
{
corrections++;
new[new_MM_pos] = line[i+old_MM_pos];
new[new_MM_pos+1] = line[i+old_MM_pos+1];
new[new_DD_pos] = line[i+old_DD_pos];
new[new_DD_pos+1] = line[i+old_DD_pos+1];
new[new_YY_pos] = line[i+old_YY_pos];
new[new_YY_pos+1] = line[i+old_YY_pos+1];
new[2] = new_P_char;
new[5] = new_P_char;
for ( j = 0; j < 8; j++ )
line[i+j] = new[j];
}
}
/*
* output possibly corrected line
*/
fprintf( out_file, "%s", line );
/*
* give progress messages to pacify the operator
*/
if ( ( lines & 0x000f ) == 0 )
show_count( lines, corrections );
}
show_count( lines, corrections );
fclose( in_file );
fclose( out_file );
printf( "\nDone\n" );
}